package com.lijie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lijie.entity.TbLoginLog;
import com.lijie.service.TbLoginLogService;
import com.lijie.utilscommon.response.Result;
import com.lijie.vo.TbLoginLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录日志表(TbLoginLog)表控制层
 *
 * @author makejava
 * @since 2020-07-31 09:36:31
 */
@Api(value = "登录日志")
@RestController
@RequestMapping("/logservice/tbLoginLog")
@CrossOrigin
public class TbLoginLogController {
    /**
     * 服务对象
     */
    @Resource
    private TbLoginLogService tbLoginLogService;

    @ApiOperation(value = "分页查询日志")
    @PostMapping("/loginlogPage/{current}/{size}")
    public Result loginlogPage(@PathVariable("current")Integer current,@PathVariable("size")Integer size,@RequestBody(required = false) TbLoginLogVO tbLoginLogVO){
        System.out.println(tbLoginLogVO.toString());

        Page<TbLoginLog> page=new Page<>(current,size);
        QueryWrapper<TbLoginLog> queryWrapper=getWrapper(tbLoginLogVO);
        return Result.ok().data("total",tbLoginLogService.findUserAndDeptCount()).data("records",tbLoginLogService.page(page,queryWrapper));
    }


    /**
     * 封装TbLoginLogVO对象
     * @param tbLoginLogVO
     * @return
     */
    public QueryWrapper<TbLoginLog> getWrapper(TbLoginLogVO tbLoginLogVO){
        QueryWrapper<TbLoginLog> queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(tbLoginLogVO.getUsername())){
            queryWrapper.like("username",tbLoginLogVO.getUsername());
        }
        if(!StringUtils.isEmpty(tbLoginLogVO.getLocation())){
            queryWrapper.like("location",tbLoginLogVO.getLocation());
        }
        if(!StringUtils.isEmpty(tbLoginLogVO.getIp())){
            queryWrapper.like("ip",tbLoginLogVO.getIp());
        }
        return  queryWrapper;
    }




    @ApiOperation(value = "逻辑删除日志登录信息")
    @DeleteMapping("/{id}")
    @CrossOrigin
    public Result delete(@PathVariable("id") Integer id) {

        boolean b = tbLoginLogService.removeById(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.error();
        }

    }


    @ApiOperation(value = "逻辑批量删除日志登录信息")
    @PostMapping("/deleteplus")
    @CrossOrigin
    public Result deleteplus(@RequestBody Integer[] ids) {
        List<Integer> id=new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            id.add(ids[i]);

        }
        boolean b = tbLoginLogService.removeByIds(id);
        if (b) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }



}